$(function(){
    var guanwang={
        data:{
            play:false
        },
        event:function () {
            var that=this;
            $(window).bind("touchmove",function (e) {
                e.preventDefault();
            });
            // 窗口事件
            $(window).bind("resize",function(){
                //如果没有播放
                if($(".play").is(":visible")){
                    $(".banner").css("backgroundPositionY",$(window).height()*0.25);
                    $(".video").css("height",$(window).height()*0.25);
                    $(".btn").css("top",$(window).height()*0.25+40);
                }
                else{
                    if(that.data.play){
                        $(".banner").css("backgroundPositionY",$(window).height());
                        $("video").css("left",($(window).width()-$("video").width())/2);
                        $(".btn").css({"top":$(window).height()});
                        $("#fp-nav").css({"top":"120%"});
                        if($(window).width()/$(window).height()>$("video").width()/$("video").height()){
                            $("video").css({"height":"100%","width":"auto"});
                            $(".video>div").css({"margin-top":0,"height":"100%"});

                        }else{
                            $("video").css({"width":"100%","height":"auto"});
                            $(".video>div").css({"margin-top":($(window).height()-$("video").height())/2,"height":"auto"});
                        }
                    }

                }
            });
            // 播放事件
            $(".play").bind("click",function () {
                that.data.play=true;
                $("#fp-nav").animate({"top":"120%"},1500);
                $(".banner .wrap").animate({"top":"100%"},1500);
                $(".banner").animate({"backgroundPositionY":$(window).height()},1500);
                $(".video_mask").fadeOut(1500);
                $(".play").fadeOut(200);
                if($(window).width()/$(window).height()>$("video").width()/$("video").height()){
                    $(".video").animate({ "height":"100%"},1500,function () {
                        $("video").animate({"height":$(window).height()},800,function () {
                            $("video").css({"width":"auto"});
                        });
                    });
                }else{
                    $(".video").animate({ "height":"100%"},1500);
                    $(".video>div").animate({"margin-top":($(window).height()-$("video").height())/2},1500);
                }



                $("video").attr("controls",true);
                $("video")[0].play();
                $.fn.fullpage.setAllowScrolling(false);
                $(".btn").animate({"top":$(window).height()},1500);
            });
            // 关闭事件
            $(".close").bind("click",function () {
                that.data.play=false;
                $("video").attr("controls",false);
                $("video")[0].pause();
                if($(".play").is(":visible")){
                    $("#fp-nav").animate({"top":"50%"},800);
                    $(".video").animate({ "height":"0%"},800);
                    $(".banner").animate({"backgroundPositionY":0},800);
                    $(".banner .wrap").animate({"top":"0"},800);
                    $(".btn").animate({"top":40},800);
                }else{
                    $(".video").animate({ "top":"-100%"},1500);
                    $.fn.fullpage.setAllowScrolling(true);
                    $("#fp-nav").animate({"top":"50%"},1500);
                    $(".btn").animate({"top":40},1500);
                    $(".banner .wrap").animate({"top":"0"},1500);
                    $(".banner").animate({"backgroundPositionY":0},1500);
                }
                $(".play").fadeOut(200);
            });
            // 菜单点击
            $(".btn").bind("click",function () {
                $(".sidebar").show().animate({"right":"0","opacity":"1"},500);
                $(this).fadeOut();
                $(".close").fadeOut();
            });
            // 关闭菜单
            $(".side_close").bind("click",function () {
                $(".close").fadeIn();
                $(".btn").fadeIn();
                $(".sidebar").animate({"right":"-100%","opacity":"0"},500,function () {
                    $(".sidebar").hide();
                });
            });
        },
        init:function () {
            var backImg=[
                {
               s:"images/p1.jpg",
               m:"images/sj1.jpg"
            },    {
               s:"images/p2.jpg",
               m:"images/sj2.jpg"
            },    {
               s:"images/p3.jpg",
               m:"images/sj3.jpg"
            },    {
               s:"images/p4.jpg",
               m:"images/sj4.jpg"
            },    {
               s:"images/p5.jpg",
               m:"images/sj5.jpg"
            },    {
               s:"images/p6.jpg",
               m:"images/sj6.jpg"
            }
            ];
           $(".section").each(function (i,o) {
               var img=$(window).width()>768?backImg[i].s:backImg[i].m;
               $(o).attr("data-img",img);
           });
            $('#fullpage').fullpage({
                verticalCentered:true,
                navigation: true,
                // anchors: ["banner", "section1", "section2", "section3", "section4", "section5", "footer"],
                onLeave: function (j, l, i) {
                    $(".wrap").hide();
                   setTimeout(function () {
                       updata();
                   },900);
                    // if(j===1){
                    //     $("#fp-nav").animate({"top":"50%"},500);
                    //     $(".btn").animate({"top":40},500);
                    // }
                    // if (j === 2) {
                    //     if(i==="up"){
                    //         if($(".play").is(":visible")){
                    //             $("#fp-nav").animate({"top":"65%"},500);
                    //             $(".btn").animate({"top":$(window).height()*0.25+40},500);
                    //         }
                    //     }
                    // }
                },
                afterLoad: function (h, i) {
                    $(".wrap").show();
                    // if(i===1){
                    //     if($(".play").is(":visible")){
                    //         $("#fp-nav").css({"top":"65%"});
                    //         $(".btn").css("top",$(window).height()*0.25+40);
                    //     }
                    //
                    // }
                }
            });
            var that=this;

            $.each($(".lazy1"),function (i,o) {
                if(o.nodeName==="IMG"){
                    $(o).attr("data-img",$(o).attr("src"));
                    $(o).attr("src","images/heibai_logo.png");
                    $(o).css({
                        "max-width":"100px",
                        "max-height":"100px"
                    });
                }else{
                    var width=$(o).width()<100?$(o).width():100;
                    var height=$(o).height()<100?$(o).height():100;
                    $(o).css({
                        backgroundSize:width+"px "+height+"px",
                        backgroundImage:"url(images/heibai_logo.png)"
                    });
                }

            });

           var updata= $('#fullpage').lazyload({
                lazyclass:"lazy1",
                distance:$(window).height(),
               callback:function ($this) {
                   if($this[0].nodeName==="IMG"){
                       $this.css({
                           "max-width":"none",
                           "max-height":"none"
                       });
                   }else{
                       $this.css({
                           backgroundSize:"cover"
                       });
                   }
               }
            });
            that.event();
               // 新闻请求
            JsonP({
                url:guanwangID+"index.php?s=home/news/listNewsQuery",
                data:{
                    id:1
                },
                success:function (res) {
                    console.log(res);
                    if(res.resStatus===1){
                        var body_string="";
                        $.each(res.data[0].articles_list,function (i,o) {
                           body_string+='<li><a href="news_detial.html?newsId='+o.article_id+'">'+o.title+'</a></li>'
                        });
                        $(".bd>ul").empty().append(body_string);
                        $(".b-news").slide({mainCell:".bd ul",autoPage:true,effect:"topLoop",autoPlay:true,vis:1});
                    }
                }
            });
              // 案例请求
           JsonP({
                url:guanwangID+"index.php?s=home/Casus/listCasusQuery",
                data:{
                },
                success:function (res) {
                    console.log(res);
                    if(res.resStatus===1){
                        var body_string="";
                        var o=res.data.casusList;
                           body_string='<div class="se2_left_wrap clearfix">' +
                               '            <div class="a-fadeinT"><div class="lazy1" data-img="'+o[0].images+'" style="background-position: center center;"><a href="anli_detial.html?caseId='+o[0].id+'"><div class="se2_bg_mask"><div><p>'+o[0].title+'</p><p>【'+o[0].scopeTitle+'】m²</p></div></div></a></div></div>\n' +
                               '            <div class="clearfix">' +
                               '                <div class="a-fadeinL1"><div class="lazy1" data-img="'+o[1].images+'" style="background-position: center center;"><a href="anli_detial.html?caseId='+o[1].id+'"><div class="se2_bg_mask"><div><p>'+o[1].title+'</p><p>【'+o[1].scopeTitle+'】m²</p></div></div></a></div></div>\n' +
                               '                <div class="a-fadeinB2"><div class="lazy1" data-img="'+o[2].images+'" style="background-position: center center;"><a href="anli_detial.html?caseId='+o[2].id+'"><div class="se2_bg_mask"><div><p>'+o[2].title+'</p><p>【'+o[2].scopeTitle+'】m²</p></div></div></a></div></div>\n' +
                               '            </div>' +
                               '        </div>' +
                               '        <div class="se2_right_wrap a-fadeinR3"><div class="lazy1" data-img="'+o[3].images+'" style="background-position: center center;"><a href="anli_detial.html?caseId='+o[3].id+'"><div class="se2_bg_mask"><div><p>'+o[3].title+'</p><p>【'+o[3].scopeTitle+'】m²</p></div></div></a></div></div>';
                        $(".se2_picture").empty().append(body_string);
                    }
                }
            });

            // loading
            $('.loading .img span i').animate({'width':'70px'},2000,function(){
            });
            $(window).bind("load",function () {
                $('.loading .img span i').animate({'width':'106px'},1000,function(){
                    $('.loading').fadeOut();
                });
            });
            // 初始
            // $(".video").css("height",$(window).height()*0.25);
            // $(".btn").css("top",$(window).height()*0.25+40);
            // $(".banner").css("backgroundPositionY",$(window).height()*0.25);
            $("#fp-nav").removeClass("right").addClass("left");
            // $("#fp-nav").css({"top":"65%"});

        }
    };

    guanwang.init();
});
